package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.exption.SignExption;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;//这个 查的是mongodb
    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    /*
    *
    * 保存/更新 医院设置
    * */
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        Map<String, Object> parameMap = getStringObjectMap(request);
        hospitalService.saveHospital(parameMap);
        return Result.ok();
    }


    // 医院设置查询
    @PostMapping("/hospital/show")
    public Result show(HttpServletRequest request) {
        Map<String, Object> stringObjectMap = getStringObjectMap(request);
        String hoscode = (String) stringObjectMap.get("hoscode");
        Hospital hospital = hospitalService.getHospitalByHoscode(hoscode);
        return Result.ok(hospital);
    }
    //通用校验
    private Map<String, Object> getStringObjectMap(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> parameMap = HttpRequestHelper.switchMap(parameterMap);
        // 签名校验
        String HospSign = (String) parameMap.get("sign");
        String hoscode = (String) parameMap.get("hoscode");
        String signKey = hospitalSetService.getSignKeyByHoscode(hoscode);
        String signKeyMD5 = MD5.encrypt(signKey);

        if(!HospSign.equals(signKeyMD5)){
            throw new SignExption("签名校验失败");
        }
        //处理图片
        String logoData = (String) parameMap.get("logoData");
        if(logoData != null){
            parameMap.put("logoData", logoData.replaceAll(" ","+"));
        }
        return parameMap;
    }


 //医院科室相关

    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> parameMap = getStringObjectMap(request);
        hospitalService.saveDepartment(parameMap);
        return Result.ok();
    }

    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request){
        Map<String, Object> paramMap = getStringObjectMap(request);
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        //非必填
        String depcode = (String)paramMap.get("depcode");
        DepartmentQueryVo departmentVo = new DepartmentQueryVo();
        departmentVo.setDepcode(depcode);
        departmentVo.setHoscode(hoscode);

        Integer pageNum =  paramMap.get("pageNum") == null ? 1 : Integer.parseInt((String) paramMap.get("pageNum"));
        Integer pageSize = paramMap.get("pageSize") == null ? 10 : Integer.parseInt((String) paramMap.get("pageSize"));
        Page<Department> page = departmentService.findAll(pageNum,pageSize,departmentVo);
        return Result.ok(page);
    }

    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request){
        Map<String, Object> paramMap = getStringObjectMap(request);
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");
        departmentService.removeDepartmentByHoscodeAndDepcode(hoscode,depcode);
        return Result.ok();
    }
 //医院科室排班相关
     @PostMapping("/saveSchedule")
     public Result saveSchedule(HttpServletRequest request){
         Map<String, Object> paramMap = getStringObjectMap(request);
         hospitalService.saveSchedule(paramMap);
         return Result.ok();
     }


     @PostMapping("/schedule/list")
     public Result findSchedule(HttpServletRequest request){
         Map<String, Object> paramMap = getStringObjectMap(request);
         String hoscode = (String)paramMap.get("hoscode");
         String depcode = (String)paramMap.get("depcode");
         Integer pageNum =  paramMap.get("pageNum") == null ? 1 : Integer.parseInt((String) paramMap.get("pageNum"));
         Integer pageSize = paramMap.get("pageSize") == null ? 10 : Integer.parseInt((String) paramMap.get("pageSize"));
         ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
         scheduleQueryVo.setDepcode(depcode);
         scheduleQueryVo.setHoscode(hoscode);
         Page<Schedule> page = hospitalService.findScheduleAll(pageNum,pageSize,scheduleQueryVo);
         return Result.ok(page);
     }

     @PostMapping("/schedule/remove")
     public Result removeSchedule(HttpServletRequest request){
         Map<String, Object> paramMap = getStringObjectMap(request);
         String hoscode = (String)paramMap.get("hoscode");
         String hosScheduleId = (String)paramMap.get("hosScheduleId");
         hospitalService.removeScheduleByHoscodeAndHosScheduleId(hoscode,hosScheduleId);
         return Result.ok();
     }
}
